extends Area2D
class_name GrabNode2DComponent

signal grabed
signal placed

@export var node2d_whitelist:Array[Node2D]
var grabed_node2d:Node2D

func _ready() -> void:
	body_entered.connect(grab)
	body_exited.connect(place)

func _physics_process(_delta: float) -> void:
	if !grabed_node2d:
		return
	grabed_node2d.global_position = global_position

func grab(body:Node2D):
	if body in node2d_whitelist:
		return
	
	grabed_node2d = body
	grabed.emit()

func stop_grab():
	node2d_whitelist.append(grabed_node2d)
	grabed_node2d = null
	monitoring = true

func place(placed_node:Node):
	if placed_node in node2d_whitelist:
		node2d_whitelist.erase(placed_node)
		set_deferred("monitoring",false)
		placed.emit()
